﻿@page "/register"
@inject IAuthService _authService

<PageTitle>Register</PageTitle>

<h3>Register</h3>

<EditForm Model="user" OnValidSubmit="HandleRegistration">
    <DataAnnotationsValidator />
    <div class="mb-3">
        <label for="email">Email</label>
        <InputText id="email" @bind-Value="user.Email" class="form-control" />
        <ValidationMessage For="@(()=>user.Email)" />
    </div>
    <div class="mb-3">
        <label for="password">Password</label>
        <InputText id="password" @bind-Value="user.Password" class="form-control" type="password" />
        <ValidationMessage For="@(()=>user.Password)" />
    </div>
    <div class="mb-3">
        <label for="confirmPassword">Confirm Password</label>
        <InputText id="confirmPassword" @bind-Value="user.ConfirmPassword" class="form-control" type="password" />
        <ValidationMessage For="@(()=>user.ConfirmPassword)" />
    </div>
    <button type="submit" class="btn btn-primary">Register</button>
    <div class="@messageCssClass">
        <span>@message</span>
    </div>

</EditForm>

@code {
    UserRegister user = new UserRegister();

    string message = string.Empty;
    string messageCssClass = string.Empty;


    async Task HandleRegistration()
    {
        var result = await _authService.Register(user);
        message = result.Message;
        if (result.Success)
        {
            messageCssClass = "text-success";
        }
        else
        {
            messageCssClass = "text-danger";
        }
    }
}
